package com.stu.action.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jedis.JedisUtil;
import jedis.ObjectUtil;
import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.stu.action.base.BaseAction;
import com.stu.entity.service.News;
import com.stu.service.service.INewsService;
import com.stu.utils.CommonUtils;
import com.stu.utils.PageBean;

@Controller
@Scope("prototype")
public class NewsAction extends BaseAction<News, INewsService> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	/**
	 * 新闻 列表
	 * index 页面 新闻模块
	 * @author lijian
	 * 
	 */
	public void list(){
		String fr=getRequest().getParameter("fr");
		List<News> newsList=null;
		PageBean pageBean=null;
		if(StringUtils.isNotBlank(fr)){
//			byte[]b=jedis.get("news".getBytes());
//			newsList=listTranscoder.deserialize(b);
//			newsList=newsList.subList(9, 14);
			/*newsList=new ArrayList<News>();
			for(int i=0;i<rows;i++){
				byte[] bytes = JedisUtil.lpop("news".getBytes());
				News news=null;
				try {
					news = (News) ObjectUtil.bytesToObject(bytes);
					news.setCreateTime(news.getCreateTime()); 
				} catch (Exception e) {
				}
				newsList.add(news);
			}*/
			try {
				newsList=jedisUtils.zrevrange("news", 0, System.currentTimeMillis(),rows);
			} catch (Exception e) {
				String hql="from News order by createTime desc";
				String title=model.getTitle();
				Map<String,Object>params=new HashMap<String,Object>(); 
				if(StringUtils.isNotBlank(title)){
					hql="from News where title like :title order by createTime desc";
					params.put("title", "%"+title+"%"); 
				}
				newsList=super.findByHql(hql, params);
				pageBean=new PageBean(page, getAccount(), rows);
			}
			if(newsList.size()<rows||null==newsList.get(0)){//如果缓存中没有获取到数据，去数据库查找
				String hql="from News order by createTime desc";
				String title=model.getTitle();
				Map<String,Object>params=new HashMap<String,Object>(); 
				if(StringUtils.isNotBlank(title)){
					hql="from News where title like :title order by createTime desc";
					params.put("title", "%"+title+"%"); 
				}
				newsList=super.findByHql(hql, params);
				pageBean=new PageBean(page, getAccount(), rows);
			}
		}else{
			String hql="from News order by createTime desc";
			String title=model.getTitle();
			Map<String,Object>params=new HashMap<String,Object>();
			if(StringUtils.isNotBlank(title)){
				hql="from News where title like :title order by createTime desc";
				params.put("title", "%"+title+"%"); 
			}
			newsList=super.findByHql(hql, params);
			pageBean=new PageBean(page, getAccount(), rows);
		}
		
		JSONObject json=new JSONObject();
		json.put("success", true);
		json.put("data", newsList); 
		json.put("pageBean", pageBean); 
		try { 
			CommonUtils.writeJson(json.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void saveNews(){
		try {
			boolean b=service.updateNews();
			JSONObject json=new JSONObject();
			json.put("success", b);
			if(b){
				logger.info("更新新闻成功");
			}else{
				logger.warn("更新新闻失败");
			}
			try { 
				CommonUtils.writeJson(json.toString());
			} catch (IOException e) {
				e.printStackTrace(); 
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
	}
	
	/**
	 * 获取 news 详情
	 * @author lijian
	 *
	 * @return
	 */
	public String getById(){
		String id=getRequest().getParameter("id");
		if(StringUtils.isNotBlank(id)){
			News news=super.findById(id);
			if(null!=news){ 
				CommonUtils.getValueStack().getContext().put("news", news);
				return "newsDetails";
			}
		}
		return "ERROR";
	}
	
	/**
	 * 根据查询条件获取记录数
	 * @author lijian
	 *
	 * @return
	 */
	public int getAccount(){
		String hql="select count(*) from News";
		String title=getRequest().getParameter("title");
		Map<String,Object>params=new HashMap<String,Object>();
		if(StringUtils.isNotBlank(title)){
			hql="select count(*) from News where title like :title ";
			params.put("title", "%"+title+"%");
		}
		return super.getAccount(hql, params);
	}
	
	/**
	 * 删除 新闻
	 * @author lijian
	 *
	 */
	public void deleteNewsByIds(){
		boolean flag=super.deleteByIds();
		JSONObject json=new JSONObject();
		json.put("success", flag);
		try { 
			CommonUtils.writeJson(json.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
